package com.biao.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.biao.auth.feign.MemberFeignService;
import com.biao.auth.vo.SocialUser;
import com.biao.auth.vo.SocialUserWB;
import com.biao.common.utils.HttpUtils;
import com.biao.common.utils.R;
import com.biao.common.vo.MemberRespVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

import static com.biao.common.constant.AuthServerConstant.LOGIN_USER;

//社交登录
@Slf4j
@Controller
public class OAuth2Controller {

    @Autowired
    private MemberFeignService memberFeignService;

    /**
     * 社交登录回调
     *
     * @param code            gitee返回的code码用来获取access_token
     * @param session
     * @param servletResponse
     */
    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session, HttpServletResponse servletResponse) throws Exception {
        Map<String, String> bodysMap = new HashMap<>();
        Map<String, String> headersMap = new HashMap<>();
        Map<String, String> querysMap = new HashMap<>();
        bodysMap.put("grant_type", "authorization_code");
        bodysMap.put("code", code);
        bodysMap.put("client_id", "9161f6fd09dc80e27885f7ac123c4439bef13114723236ed9da7a20c861518f1");
        bodysMap.put("redirect_uri", "http://auth.mall.com/oauth2.0/gitee/success");
        bodysMap.put("client_secret", "c489089377da08e730f58fec02ef89e74ba7726660171d076a34238af6d06965");
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "POST", headersMap, querysMap, bodysMap);
        if (!(response.getStatusLine().getStatusCode() == 200)) {//出错
            return "redirect:http://auth.mall.com/login.html";
        }
        HttpEntity entity = response.getEntity();
        String json = EntityUtils.toString(response.getEntity());
        SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
        R r = memberFeignService.oauth2login(socialUser);
        if (!(r.getCode() == 0)) {//登录失败
            return "redirect:http://auth.mall.com/login.html";
        }

        MemberRespVo memberRespVo = r.getData(new TypeReference<MemberRespVo>() {
        });
        log.info("登录成功用户信息:{}", memberRespVo.toString());

        session.setAttribute(LOGIN_USER, memberRespVo);

        return "redirect:http://mall.com";
    }


    /**
     * 微博社交登录回调
     *
     * @param code
     * @param session
     * @param servletResponse
     */
    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpSession session, HttpServletResponse servletResponse) throws Exception {
        Map<String, String> bodysMap = new HashMap<>();
        Map<String, String> headersMap = new HashMap<>();
        Map<String, String> querysMap = new HashMap<>();
        bodysMap.put("grant_type", "authorization_code");
        bodysMap.put("code", code);
        bodysMap.put("client_id", "2371891817");
        bodysMap.put("redirect_uri", "http://auth.mall.com/oauth2.0/weibo/success");
        bodysMap.put("client_secret", "30a75bd512c1d7296ba69dd5f3b3d053");
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "POST", headersMap, querysMap, bodysMap);
        if (!(response.getStatusLine().getStatusCode() == 200)) {//出错
            return "redirect:http://auth.mall.com/login.html";
        }
        HttpEntity entity = response.getEntity();
        String json = EntityUtils.toString(response.getEntity());
        SocialUserWB socialUser = JSON.parseObject(json, SocialUserWB.class);
        R r = memberFeignService.oauth2loginWB(socialUser);
        if (!(r.getCode() == 0)) {//登录失败
            return "redirect:http://auth.mall.com/login.html";
        }

        MemberRespVo memberRespVo = r.getData(new TypeReference<MemberRespVo>() {
        });
        log.info("登录成功用户信息:{}", memberRespVo.toString());

        session.setAttribute(LOGIN_USER, memberRespVo);

        return "redirect:http://mall.com";
    }

    @ResponseBody
    @GetMapping("/oauth2.0/weibo/error")
    public String weiboError(@RequestParam("code") String code, HttpSession session, HttpServletResponse servletResponse) throws Exception {
        return "微博登录失败";
    }
}